﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ApplicationLibrary.DataBase;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace ApplicationLibrary.CounterManager
{
    public partial class codesForm : DocumentForm
    {
        private ComboBoxManager cbbManager;
        private EnumsManager<InputType> eSegmentType = null;

        public ComboBox GetCode { get { return cbbCode; } }

        public TextBox GetDescription { get { return txtDescription; } }

        public ExtendedDataGridView GetDataGrid { get { return dgwSegments; } }

        public ComboBoxManager GetCbbManager { get { return cbbManager; } }

        public codesForm()
            : base("codes")
        {
            InitializeComponent();
            InitializeAuxComponent();
        }

        #region Virtual Method

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerCodes("codesForm", new RadarCodes(), this);
            dbManager.AddMaster(this, AM_Codes.Name, true);
            dbManager.AddSlave(AM_CodeSegment.Name, true);
            dbManager.AddRelation("CodeSegment", AM_Codes.CodeType, AM_CodeSegment.CodeType);

            dgwSegments.AutoGenerateColumns = false;
            dgwSegments.DataSource = dbManager.SlaveBinding("CodeSegment");

            sgmSegmentNo.DataPropertyName = AM_CodeSegment.Segment.Name;
            sgmCodeType.DataPropertyName = AM_CodeSegment.InputType.Name;
            sgmLength.DataPropertyName = AM_CodeSegment.InputLen.Name;
            sgmHeader.DataPropertyName = AM_CodeSegment.Description.Name;
        }

        protected override void OnBindData()
        {
            BindControl(cbbCode, AM_Codes.CodeType, "SelectedValue", NullValue.NotSet);
            BindControl(txtDescription, AM_Codes.Description);
            BindControl(dgwSegments);

            BindColumn(sgmSegmentNo, AM_CodeSegment.Segment);
            BindColumn(sgmCodeType, AM_CodeSegment.InputType);
            BindColumn(sgmLength, AM_CodeSegment.InputLen);
            BindColumn(sgmHeader, AM_CodeSegment.Description);

            dgwSegments.RowIndex = AM_CodeSegment.Segment;
        }

        protected override void OnDisableControlsForEdit()
        {
            cbbCode.Enabled = false;
            txtDescription.Enabled = false;
            base.OnDisableControlsForEdit();
        }

        protected override void OnDisableControlsForNew()
        {
        }

        protected override void OnPrepareAuxData()
        {
        }

        protected override void FocusOnNew()
        {
            cbbCode.Focus();
        }

        protected override bool OnBeforeSave()
        {
            return base.OnBeforeSave();
        }

        #endregion

        public int AddNewRow(string desc, int inputType, int inputLen)
        {
            DataRowView drw = dgwSegments.AddNewRow(AM_CodeSegment.Segment);

            DataRowViewValues.SetValue<string>(drw, AM_CodeSegment.Description, desc);
            DataRowViewValues.SetValue<int>(drw, AM_CodeSegment.InputType, inputType);
            DataRowViewValues.SetValue<int>(drw, AM_CodeSegment.InputLen, inputLen);

            return DataRowViewValues.GetValue<int>(drw, AM_CodeSegment.Segment);
        }

        private void InitializeAuxComponent()
        {
            cbbManager = new ComboBoxManager();
            cbbManager.CreateList<string>();
            foreach (KeyValuePair<string, string> codes in GlobalInfo.CodeTypes)
                cbbManager.AddValue(codes.Key, codes.Value);
            cbbManager.AttachTo(cbbCode);
            eSegmentType = new EnumsManager<InputType>(sgmCodeType, Properties.Resources.ResourceManager);
        }

        private void cbbCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDescription.Text = cbbCode.Text;
        }
    }

    #region dbmanagerCodes

    internal class dbmanagerCodes : ApplicationLibrary.DBManager.DBManager
    {
        public dbmanagerCodes(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_RowUpdating(sender, e);
        }

        protected override void dAdapter_RowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                e.Row[AM_CodeSegment.CodeType.Name] = GetColumn<string>(AM_Codes.CodeType, Pos);
            }
            base.dAdapter_RowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new AM_Codes();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(AM_Codes.CodeType, "@p1");
            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter nParam = new SqlABParameter("@p1", AM_Codes.CodeType);
            nParam.Value = 0;
            PList.Add(nParam);

            return PList;
        }

        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }

        protected override string CreateSlaveQuery(string name)
        {
            if (name == AM_CodeSegment.Name)
            {
                AM_CodeSegment CodeSegment = new AM_CodeSegment();
                QueryBuilder qB = new QueryBuilder();
                qB.AddSelect();
                qB.AddFrom(AM_CodeSegment.Name);
                qB.AddCompare(AM_CodeSegment.CodeType, "@p2");
                qB.AddOrder(AM_CodeSegment.Segment);
                return qB.Query;
            }

            return "";
        }

        protected override List<SqlABParameter> CreateSlaveParam(string name)
        {
            if (name == AM_CodeSegment.Name)
            {
                List<SqlABParameter> PList = new List<SqlABParameter>();

                SqlABParameter sParam = new SqlABParameter("@p2", AM_CodeSegment.CodeType);
                sParam.Value = 0;
                PList.Add(sParam);
                return PList;
            }
            return null;
        }
    }

    #endregion
}